package com.langchain4j.simple.config;


import com.langchain4j.simple.service.FunctionAssistant;
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.request.json.JsonObjectSchema;
import dev.langchain4j.model.chat.request.json.JsonStringSchema;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.tool.ToolExecutor;
import dev.langchain4j.service.tool.ToolProvider;
import dev.langchain4j.service.tool.ToolProviderRequest;
import dev.langchain4j.service.tool.ToolProviderResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName LangChainConfig
 * @createTime 10:53:00 2025/04/28
 */
@Slf4j
@Configuration(proxyBeanMethods = false)
public class LangChainConfig {

    @Bean
    public ChatModel chatLanguageModel() {
        return OpenAiChatModel.builder()
                .apiKey(System.getenv("DASHSCOPE_KEY"))
                .modelName("qwen-turbo")  // 设置使用的模型名称
                .logRequests(true)
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .build();
    }

    @Bean
    public FunctionAssistant functionAssistant(ChatModel chatLanguageModel) {
        // 工具说明 ToolSpecification
        JsonObjectSchema jsonObjectSchema = JsonObjectSchema.builder()
                .addProperty("companyName", JsonStringSchema.builder().description("公司名称").build())
                .addProperty("dutyNumber", JsonStringSchema.builder().description("税号").build())
                .addProperty("amount", JsonStringSchema.builder().description("金额，保留两位有效数字").build())
                //.required("expression")  // 必填字段
                .build();

        ToolSpecification toolSpecification = ToolSpecification.builder()
                .name("invoice_assistant")
                .description("根据用户提交的开票信息，开具发票")
                .parameters(jsonObjectSchema)
                .build();

        // 业务逻辑 ToolExecutor
        ToolExecutor toolExecutor = (toolExecutionRequest, memoryId) -> {
            String arguments1 = toolExecutionRequest.arguments();
            System.out.println("arguments1 =>>>> " + arguments1);
            return "开具成功";
        };

        return AiServices.builder(FunctionAssistant.class)
                .chatModel(chatLanguageModel)
//                .tools(Map.of(toolSpecification, toolExecutor))
//                .tools(new InvoiceHandler())
                // 动态工具配置
                .toolProvider(new ToolProvider() {
                    @Override
                    public ToolProviderResult provideTools(ToolProviderRequest request) {
                        UserMessage userMessage = request.userMessage();
                        // 含有发票关键字 ，
                        if (userMessage.singleText().contains("booking")) {
                            ToolSpecification toolSpecification = ToolSpecification.builder()
                                    .name("get_booking_details")
                                    .description("Returns booking details")
                                    .parameters(JsonObjectSchema.builder()
                                            .addProperty("bookingNumber", JsonStringSchema.builder().description("公司名称").build())
                                            .build())
                                    .build();
                            return ToolProviderResult.builder()
                                    .add(toolSpecification, toolExecutor)
                                    .build();
                        } else {
                            return null;
                        }
                    }
                })
                .build();
    }
}
